package com.obelisco.vista.controles;

import java.lang.reflect.InvocationTargetException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.Format;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Locale;

import javax.swing.text.NumberFormatter;

import nextapp.echo2.app.ApplicationInstance;

import org.apache.commons.beanutils.PropertyUtils;

import com.jgoodies.binding.adapter.BasicComponentFactory;
import com.jgoodies.binding.adapter.echo.TextComponentConnector;
import com.jgoodies.binding.beans.PropertyAdapter;
import com.jgoodies.binding.value.ConverterFactory;
import com.jgoodies.binding.value.ValueModel;

import echopointng.KeyStrokeListener;

public class BindingNumericTextField extends NumericTextField {

	//ValueModel model = null;
	PropertyAdapter model = null;

	ValueModel model2 = null;

	TextComponentConnector connector = null;

	public BindingNumericTextField(int ancho) { 
		super(ancho);
		// TODO Auto-generated constructor stub
	}

	public BindingNumericTextField(int ancho, Object object, String property) {
		this(ancho);
		
		setBinding(object, property);
	}
	
	private void addKeyCombinations(KeyStrokeListener ks, int keyCombo) {
		String keyName = KeyStrokeListener.getMnemonic(keyCombo);

//		final int ALT = KeyStrokeListener.ALT_MASK;
//		final int SHIFT = KeyStrokeListener.SHIFT_MASK;
//		final int CTRL = KeyStrokeListener.CONTROL_MASK;

		ks.addKeyCombination(keyCombo, keyName);
//		ks.addKeyCombination(SHIFT | keyCombo);
//		ks.addKeyCombination(ALT | keyCombo);
//		ks.addKeyCombination(CTRL | keyCombo);
//		ks.addKeyCombination(SHIFT | CTRL | keyCombo);
//		ks.addKeyCombination(ALT | SHIFT | CTRL | keyCombo);
//		ks.addKeyCombination(ALT | CTRL | keyCombo);
	}
	
	private KeyStrokeListener createKSL() {

		KeyStrokeListener ks = new KeyStrokeListener();
//		for (int i = KeyStrokeListener.VK_A; i <= KeyStrokeListener.VK_Z; i++) {
//			addKeyCombinations(ks, i);
//		}
//		for (int i = KeyStrokeListener.VK_F1; i <= KeyStrokeListener.VK_F19; i++) {
//			addKeyCombinations(ks, i);
//		}
		for (int i = KeyStrokeListener.VK_0; i <= KeyStrokeListener.VK_9; i++) {
			addKeyCombinations(ks, i);
		}
		//KeyStrokeListener.VK_COMMA;
		ks.addKeyCombination(KeyStrokeListener.VK_PERIOD);
		ks.addKeyCombination(KeyStrokeListener.VK_COMMA);
//		ks.addKeyCombination(KeyStrokeListener.VK_NUM_LOCK);
//		ks.addKeyCombination(KeyStrokeListener.VK_ESCAPE);
//		ks.addKeyCombination(KeyStrokeListener.VK_ALT);
//		ks.addKeyCombination(KeyStrokeListener.VK_CONTROL);
//		ks.addKeyCombination(KeyStrokeListener.VK_SHIFT);
//		ks.addKeyCombination(KeyStrokeListener.VK_TAB);
//		ks.addKeyCombination(KeyStrokeListener.VK_BACK_SPACE);
//		ks.addKeyCombination(KeyStrokeListener.VK_BACK_QUOTE);
//		ks.addKeyCombination(KeyStrokeListener.VK_BACK_SLASH);
//		ks.addKeyCombination(KeyStrokeListener.VK_SPACE);
//		ks.addKeyCombination(KeyStrokeListener.VK_END);
//		ks.addKeyCombination(KeyStrokeListener.VK_HOME);
//		ks.addKeyCombination(KeyStrokeListener.VK_PAGE_UP);
//		ks.addKeyCombination(KeyStrokeListener.VK_PAGE_DOWN);
//		ks.addKeyCombination(KeyStrokeListener.VK_INSERT);
//		ks.addKeyCombination(KeyStrokeListener.VK_DELETE);
//		ks.addKeyCombination(KeyStrokeListener.VK_ENTER);
//		ks.addKeyCombination(KeyStrokeListener.VK_RETURN);
//		ks.addKeyCombination(KeyStrokeListener.VK_UP);
//		ks.addKeyCombination(KeyStrokeListener.VK_DOWN);
//		ks.addKeyCombination(KeyStrokeListener.VK_LEFT);
//		ks.addKeyCombination(KeyStrokeListener.VK_RIGHT);
		// ks.setCancelMode(true);
		return ks;
	}


	public void setBinding(Object object, String property) {

		try {
			Format fmt = null;

			Class clase = PropertyUtils.getPropertyType(object, property);
			if ("java.lang.Long".equals(clase.getName())
					|| "java.lang.Integer".equals(clase.getName())
					|| "java.lang.Short".equals(clase.getName())
					|| "java.lang.Byte".equals(clase.getName())) {
							

				//fmt = NumberFormat.getIntegerInstance(ApplicationInstance.getActive().getLocale());
				fmt = NumberFormat.getIntegerInstance(new Locale("es","VE"));
				
				//fmt = NumberFormat.getNumberInstance(new Locale("es","VE"));
				// NumberFormat numFmt = (NumberFormat) fmt;
				// fmt = NumberFormat.getInstance();
				// this.setFormat(NumericTextField.INTEGER_FORMAT);
			} else if ("java.lang.Double".equals(clase.getName())
					|| "java.lang.Float".equals(clase.getName())) {
				//fmt = NumberFormat.getNumberInstance(ApplicationInstance.getActive().getLocale());
				// fmt = NumberFormat.getNumberInstance(new Locale("es","VE"));

				fmt = new DecimalFormat("0.000");
				NumberFormat numFmt = (NumberFormat) fmt;
				numFmt.setParseIntegerOnly(false);
				
				//fmt = NumberFormat.getInstance();
				//this.setFormat(NumericTextField.DECIMAL_FORMAT);
			}
			
			if (connector != null) {
				connector.release();
			}
			if (model == null) {
				model = new PropertyAdapter(object, property, true);
			} else {
				model.setBean(object);
			}

						
			model2 = ConverterFactory.createStringToNumberConverter(model,fmt,clase);
			//model2 = ConverterFactory.createLongConverter(arg0, arg1)
			connector = new TextComponentConnector(model2, this);
			connector.updateTextComponent();

		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
